//
//  BaseCacheManager+NSCoding.m
//  Project
//
//  Created by Suny on 14-8-11.
//  Copyright (c) 2014年 Suny. All rights reserved.
//

#import "BaseCacheManager+NSCoding.h"
#import "NSString+BaseFramework.h"

@implementation BaseCacheManager (NSCoding)

- (void)saveObject:(NSObject <NSCoding> *)object toFile:(NSString *)fileName storePath:(NSString *)path
{
    [BaseCacheManager saveObject:object toFile:fileName storePath:path];
}

+ (void)saveObject:(NSObject <NSCoding> *)object toFile:(NSString *)fileName storePath:(NSString *)path
{
    dispatch_async(get_cache_io_queue(), ^{
        if (object != nil && fileName != nil)
        {
            NSString * fullPath = [path stringByAppendingPathComponent:fileName];
            @synchronized(fullPath)
            {
                [NSKeyedArchiver archiveRootObject:object toFile:fullPath];
            }
        }
    });
}

- (void)saveObject:(NSObject<NSCoding> *)object toFilePath:(NSString *)fullFilePath
{
       [BaseCacheManager saveObject:object toFilePath:fullFilePath];
}

+ (void)saveObject:(NSObject<NSCoding> *)object toFilePath:(NSString *)fullFilePath
{
	dispatch_async(get_cache_io_queue(), ^{
        if (object && fullFilePath)
        {
            @synchronized(fullFilePath)
            {
                [NSKeyedArchiver archiveRootObject:object toFile:fullFilePath];
            }
        }
    });
}

- (id)loadObjectFromFile:(NSString *)fileName storePath:(NSString *)path
{
    return [BaseCacheManager loadObjectFromFile:fileName storePath:path];
}

+ (id)loadObjectFromFile:(NSString *)fileName storePath:(NSString *)path
{
    id ret = nil;
	
    @try
	{
        if (fileName != nil)
        {
            NSString *fullPath = [path stringByAppendingPathComponent:fileName];
            @synchronized(fullPath)
            {
                ret = [NSKeyedUnarchiver unarchiveObjectWithFile:fullPath];
            }
        }
    }
    @catch (NSException *exception)
	{
        DLogObject(exception);
    }
    @finally
	{
        return ret;
    }
}

- (id)loadObjectFromFilePath:(NSString *)fullFilePath
{
    return  [BaseCacheManager loadObjectFromFilePath:fullFilePath];
}
+ (id)loadObjectFromFilePath:(NSString *)fullFilePath
{
    id ret = nil;
	@try {
        if (fullFilePath)
        {
            @synchronized(fullFilePath)
            {
                ret = [NSKeyedUnarchiver unarchiveObjectWithFile:fullFilePath];
            }
        }
	}
    @catch (NSException *exception)
    {
        DLogObject(exception);
    }
    @finally
    {
        return ret;
    }
}

@end
